package ar.com.personalTravellers.rowProcessors;

import ar.com.personalTravellers.beans.Contact;
import ar.com.personalTravellers.beans.Country;
import ar.com.personalTravellers.beans.Hotel;
import ar.com.personalTravellers.daos.factory.PTDAOFactory;
import com.angel.base.helpers.StringHelper;
import com.angel.io.exceptions.InvalidRowDataException;
import com.angel.io.parameters.ParametersService;
import com.angel.io.processors.imports.ImportRowProcessorCommand;
import com.angel.io.type.rows.impl.HeaderRowFile;
import com.angel.io.type.rows.interfaces.ImportRowFile;

/**
 *
 * @author William
 */
public class HotelContactsRowProcessorCommand implements ImportRowProcessorCommand {

    public static final String HOTEL_NAME_COLUMN = "hotelName";
	public static final String CONTACT_NAME_COLUMN = "contactName";
    public static final String CONTACT_SURNAME_COLUMN = "contactSurname";
    public static final String CONTACT_EMAIL_COLUMN = "email";
    public static final String CONTACT_TELEPHONE_COLUMN = "telephone";
    public static final String CONTACT_COMMENT_COLUMN = "comment";
    public static final String CONTACT_COUNTRY_COLUMN = "country";

    public static final String DAO_FACTORY_PARAMETER_SERVICE_NAME = "daoFactoryHotelContacts";

    public void checkRowData(ImportRowFile importRowFile, HeaderRowFile headerRowFile) throws InvalidRowDataException {
		String hotelName = importRowFile.getStringValue(headerRowFile.getColumnPosition(HOTEL_NAME_COLUMN));
        String comment = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTACT_COMMENT_COLUMN));
        String email = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTACT_EMAIL_COLUMN));
        String name = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTACT_NAME_COLUMN));
        String surname = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTACT_SURNAME_COLUMN));
        String telephone = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTACT_TELEPHONE_COLUMN));
        String countryName = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTACT_COUNTRY_COLUMN));

        Boolean areAllNotEmpty = StringHelper.areAllNotEmpty(hotelName, comment, email, name, surname, telephone, countryName);
        if( !areAllNotEmpty){
			throw new InvalidRowDataException("Some row data are NULL - " +
					"comment: [" + comment + "] - " +
                    "email: [" + email + "] - " +
                    "name: [" + name + "] - " +
                    "surname: [" + surname + "] - " +
					"hotelName: [" + hotelName  + "].");
		}
    }

    public Object processRow(ImportRowFile importRowFile, HeaderRowFile headerRowFile, ParametersService parametersService) {
        String hotelName = importRowFile.getStringValue(headerRowFile.getColumnPosition(HOTEL_NAME_COLUMN));
        String comment = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTACT_COMMENT_COLUMN));
        String email = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTACT_EMAIL_COLUMN));
        String name = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTACT_NAME_COLUMN));
        String surname = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTACT_SURNAME_COLUMN));
        String telephone = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTACT_TELEPHONE_COLUMN));
        String countryName = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTACT_COUNTRY_COLUMN));

        PTDAOFactory daoFactory = (PTDAOFactory) parametersService.findParameter(DAO_FACTORY_PARAMETER_SERVICE_NAME);

        Country country = daoFactory.getCountryDAO().findUniqueByName(countryName);
        Hotel hotel = daoFactory.getHotelDAO().findUniqueByName(hotelName);

        Contact contact = new Contact();
        contact.setCountry(country);
        contact.setEmail(email);
        contact.setName(name);
        contact.setSurname(surname);
        contact.setTelephone(telephone);

        hotel.addContact(contact);
        return hotel;
    }

    public String[] getHeaderColumnsNames() {
        return new String[] {
            HOTEL_NAME_COLUMN,
            CONTACT_COMMENT_COLUMN,
            CONTACT_EMAIL_COLUMN,
            CONTACT_NAME_COLUMN,
            CONTACT_SURNAME_COLUMN,
            CONTACT_TELEPHONE_COLUMN
        };
    }
}